package org.jasig.cas.authentication.handler;

import org.jasig.cas.util.Digests;
import org.jasig.cas.util.Encodes;
import org.jasig.cas.util.FinalValue;
import org.jasig.cas.util.PasswordUtil;

public class DigestsPasswordEncode implements PasswordEncoder {
	
	private final int HASH_INTERATIONS=FinalValue.HASH_INTERATIONS;

	@Override
	public String encode(String password) {
		String [] ps=new String[2];
		if(password!=null){
			if ((password.toString().lastIndexOf("{") == -1) || (password.toString().lastIndexOf("}") == -1)) {
	            throw new IllegalArgumentException("密码要求必须使用盐值密码！");
	        }
		}else{
			throw new IllegalArgumentException("密码不允许为空！");
		}
		ps=PasswordUtil.demergePasswordAndSalt(password);
		byte[] bytepassword=Digests.sha1(ps[0].getBytes(), Encodes.decodeHex(ps[1]),this.HASH_INTERATIONS);
		return Encodes.encodeHex(bytepassword);
	}

	public int getHASH_INTERATIONS() {
		return HASH_INTERATIONS;
	}

}
